package com.wlstock.funi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.wlstock.funi.R;
import com.wlstock.funi.model.OpBSModel;
import com.wlstock.funi.model.OperateModel;
import com.wlstock.funi.utils.CaculateUtils;
import com.wlstock.funi.utils.TimeUtils;

import java.util.ArrayList;


public class OpBSAdapter extends BaseAdapter implements OnClickListener {
	private Context mContext;
	private ArrayList<OpBSModel> models;
	public 	onOpItemClick   opBtnClick;
	public OpBSAdapter(Context mContext){
		super();
		this.mContext = mContext;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return models.size();
	}

	@Override
	public Object getItem(int index) {
		// TODO Auto-generated method stub
		return models.get(index);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int index, View converView, ViewGroup viewGroup) {
		Holder holder = null;
		if (converView == null) {
			converView = LayoutInflater.from(mContext).inflate(R.layout.list_item_tside_operate_mode, viewGroup, false);
			holder = new Holder();
			holder.tv_person_name = (TextView) converView.findViewById(R.id.tv_person_name);
			holder.tv_session_profit = (TextView) converView.findViewById(R.id.tv_session_profit);
			holder.tv_his_profit  = (TextView) converView.findViewById(R.id.tv_his_profit);
			holder.tv_opType    = (TextView) converView.findViewById(R.id.tv_opType);
			holder.tv_stk_info = (TextView) converView.findViewById(R.id.tv_stk_info);
			holder.layout_op_buy = converView.findViewById(R.id.layout_op_buy);
			holder.layout_op_saile = converView.findViewById(R.id.layout_op_saile);
			
			holder.btn_op_type_new = (Button) holder.layout_op_buy.findViewById(R.id.btn_op_type);
			holder.tv_stk_info_new = (TextView) holder.layout_op_buy.findViewById(R.id.tv_stk_info);
			holder.tv_op_price_new = (TextView) holder.layout_op_buy.findViewById(R.id.tv_op_price);
			holder.tv_op_time_new = (TextView) holder.layout_op_buy.findViewById(R.id.tv_op_time);
			holder.tv_update_time_new = (TextView) holder.layout_op_buy.findViewById(R.id.tv_update_time);
			
			holder.btn_op_type_second = (Button) holder.layout_op_saile.findViewById(R.id.btn_op_type);
			holder.tv_stk_info_second = (TextView) holder.layout_op_saile.findViewById(R.id.tv_stk_info);
			holder.tv_op_price_second = (TextView) holder.layout_op_saile.findViewById(R.id.tv_op_price);
			holder.tv_op_time_second = (TextView) holder.layout_op_saile.findViewById(R.id.tv_op_time);
			holder.tv_update_time_second = (TextView) holder.layout_op_saile.findViewById(R.id.tv_update_time);
			
			converView.setTag(holder);
		} else {
			holder = (Holder) converView.getTag();
		}
		
		OpBSModel model = models.get(index);
		holder.tv_person_name.setText(model.fundName);
		holder.tv_session_profit.setText("本期收益:"+CaculateUtils.getPercentByFloat(model.ProfitRate));
		holder.tv_his_profit.setText("历史收益:"+CaculateUtils.getPercentByFloat(model.totalIncome));
		
		if (model.isEmpty()) {
			//空仓
			holder.tv_opType.setText("空仓");
			holder.layout_op_buy.setVisibility(View.GONE);
			holder.layout_op_saile.setVisibility(View.GONE);
		} else {
			OperateModel newlyModel =  model.getNewlyOPModel();
			if (newlyModel!=null) {
				holder.tv_opType.setText(newlyModel.getRemark()+":");
				holder.tv_stk_info.setText(newlyModel.getStockName()+"("+newlyModel.getStockNo()+")");
				
				setBtnView(holder.btn_op_type_new, newlyModel.getTradeType());
				holder.tv_stk_info_new.setText(newlyModel.getRemark()+newlyModel.getStockName()+"("+newlyModel.getStockNo()+")");
				holder.tv_op_price_new.setText("成本" + newlyModel.getTradePrice());
				holder.tv_op_time_new.setText(newlyModel.getTradedDay()+" "+TimeUtils.parseHHMM(newlyModel.getTradedDate()));
				holder.tv_update_time_new.setText("更新:"+newlyModel.getTradedDay()+TimeUtils.parseHHMM(newlyModel.getCreatedTime()));
				
				holder.btn_op_type_new.setTag(newlyModel);
				holder.btn_op_type_new.setOnClickListener(this);
				
			}else {
				holder.layout_op_buy.setVisibility(View.GONE);
			}

			OperateModel secondModel = model.getSecondOPModel();
			if (secondModel!=null) {
				
				setBtnView(holder.btn_op_type_second, secondModel.getTradeType());
				holder.tv_stk_info_second.setText(secondModel.getRemark()+secondModel.getStockName()+"("+secondModel.getStockNo()+")");
				holder.tv_op_price_second.setText("成本" + secondModel.getTradePrice());
				holder.tv_op_time_second.setText(secondModel.getTradedDay()+" "+TimeUtils.parseHHMM(secondModel.getTradedDate()));
				holder.tv_update_time_second.setText("更新:"+secondModel.getTradedDay()+TimeUtils.parseHHMM(secondModel.getCreatedTime()));
				
				holder.btn_op_type_second.setTag(secondModel);
				holder.btn_op_type_second.setOnClickListener(this);
			} else {
				holder.layout_op_saile.setVisibility(View.GONE);
			}
		}
		
		return converView;
	}
	
	private void setBtnView(Button btn, int tradeType) {
		// TODO 根据不同方向设置btn的样式
		if (tradeType == OperateModel.TradeType_BUY) {
			btn.setBackgroundResource(R.drawable.btn_buy);
		}else {
			btn.setBackgroundResource(R.drawable.btn_saile);
		}
		
	}

	public ArrayList<OpBSModel> getModels() {
		return models;
	}

	public void setModels(ArrayList<OpBSModel> models) {
		this.models = models;
	}

	private class Holder{
		
		TextView tv_person_name;
		TextView tv_session_profit;
		TextView tv_his_profit;
		TextView tv_opType;
		TextView tv_stk_info;
		
		View layout_op_buy;
		View layout_op_saile;
		
		Button btn_op_type_new;
		TextView tv_stk_info_new;
		TextView tv_op_price_new;
		TextView tv_op_time_new;
		TextView tv_update_time_new;
		
		Button btn_op_type_second;
		TextView tv_stk_info_second;
		TextView tv_op_price_second;
		TextView tv_op_time_second;
		TextView tv_update_time_second;
	}

	@Override
	public void onClick(View v) {
		OperateModel model = (OperateModel) v.getTag();
		if (opBtnClick!=null) {
			opBtnClick.click(v, model);
		}
	}
	
	public interface onOpItemClick{
		public void click(View v, OperateModel model);
	}
	
}
